package wsi.exp.exp3;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.rygh.core.xml.XmlLeaf;
import org.rygh.core.xml.XmlParser;
import org.rygh.core.xml.XmlPath;
import org.rygh.core.xml.XmlTree;
import org.rygh.semantic.model.ex.PredicatePath;

import wsi.exp.ExpXml.Tag;

public class GoldSetMatch extends PathMatch {

	public GoldSetMatch() {

	}

	public int getGoldSizeByPair() {
		return pairs.size();
	}

	public int getGoldSizeByPred() {
		return predicatePathSet.size();
	}

	public List<PathMatchPair> getUnhit(Set<PathMatchPair> hitPairSet) {
		List<PathMatchPair> list = new ArrayList<>();

		for (PathMatchPair i : pairs)
			if (!hitPairSet.contains(i))
				list.add(i);

		return list;
	}

	@Deprecated
	@Override
	public void load(String content) {

		this.content = content;

		String[] lines = content.split("\r\n");

		PredicatePath path = null;
		XmlPath xpath = null;
		PathMatchPair pair;

		for (int i = 0; i < lines.length; i++) {
			String line = lines[i].trim();

			if (line.isEmpty() || line.startsWith("// "))
				continue;

			if (line.charAt(0) != '/') {
				path = new PredicatePath(line);
				continue;
			} else
				xpath = new XmlPath(line);

			if (path == null || xpath == null)
				continue;

			pair = new PathMatchPair(path, xpath);
			add(pair);
		}
	}

	@Override
	public void loadXml(String content) {
		this.content = content;

		XmlParser parser = new XmlParser(content);
		XmlTree xmltree = parser.parse();

		assert parser.isValid();
		assert xmltree != null;

		PredicatePath path = null;
		XmlPath xpath = null;
		PathMatchPair pair;

		List<XmlLeaf> nodes = xmltree.getNodesByXPath(XmlPath.compose(
				Tag.result, Tag.matches, Tag.match));

		for (XmlLeaf node : nodes) {
			path = new PredicatePath(node
					.getChildByName(Tag.predicate_path.name()).getValue()
					.getValue());
			xpath = new XmlPath(node.getChildByName(Tag.xpath.name())
					.getValue().getValue());

			pair = new PathMatchPair(path, xpath);

			add(pair);
		}
	}

}
